package com.apache.bid.controller.admin;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.apache.api.api.ApiService;
import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.bid.controller.RestfullBaseAction;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.passport.entity.Token;
import com.apache.rpc.client.RpcProtoclClient;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;

/**
 * description: Http或Https访问action
 * 
 */
@Controller
@RequestMapping("wbd/service/")
public class ResultfullWbdAdminAction extends RestfullBaseAction {

	@Autowired
	private ApiService wbdService;

	protected ApiService getApiService() {
		return wbdService;
	}

	/**
	 * 登录
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public void login(HttpServletRequest request, HttpServletResponse response) {
		ResultMsg msg = new ResultMsg("F", "登录失败");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		Map<String, Object> maps = new HashMap<String, Object>();
		try {
			if (StrUtil.isNull(password) || StrUtil.isNull(username)) {
				msg = new ResultMsg("F", "用户名或密码不能为空");
			} else {
				String sysEname = ToolsUtil.getInstance().getValueByKey("sysEname");
				maps.put("userEname", username);
				maps.put("userPass", password);
				maps.put("sysEname", sysEname);
				maps.put("addressIP", request.getRemoteAddr());
				ResultEntity entity = RpcProtoclClient.getInstance().doService("ssoService",
						"outSideSocketLoginService", maps, getInterfaceRegister());
				if (("true".equals(entity.getResult())) && !ToolsUtil.isEmpty(entity.getEntity())) {
					if (entity.getEntity() instanceof String) {
						msg.setMsg(String.valueOf(entity.getMessage()));
					} else {
						Token token = (Token) entity.getEntity();
						Cookie cookie = new Cookie(SystemTools.getInstance().getValue("cookieName"), token.getTokenId());
						cookie.setPath("/");
						cookie.setMaxAge(-1);
						Cookie ucsso = new Cookie("_uc.sso", token.getUserEname());
						ucsso.setPath("/");
						ucsso.setMaxAge(-1);
						response.addCookie(cookie);
						response.addCookie(ucsso);
						request.getSession().removeAttribute("loginUser");
						request.getSession().removeAttribute("tokenId");
						request.getSession().setAttribute("tokenId", token.getTokenId());
						msg = new ResultMsg("T", token.getTokenId());
					}
				}
			}
			String callback = request.getParameter("callback");
			PrintWriter out = response.getWriter();
			if (Validator.isNull(callback)) {
				response.setContentType("text/html; charset=UTF-8");
				out.print(JSONObject.fromObject(msg).toString());
				out.flush();
			} else {
				response.setContentType("application/javascript");
				out.print(callback + "(" + JSONObject.fromObject(msg).toString() + ")");
				out.flush();
			}
		} catch (Exception e) {
		}
	}

	private InterfaceRegister getInterfaceRegister() {
		InterfaceRegister register = new InterfaceRegister();
		String type = ToolsUtil.getInstance().getValueByKey("req_type");
		if ("socket".equals(type)) {
			String ip = ToolsUtil.getInstance().getValueByKey("check_url");
			String port = ToolsUtil.getInstance().getValueByKey("check_port");
			register.setAddress(ip);
			register.setPort(port);
			register.setCallType("socket");
		} else {
			String ssoPath = ToolsUtil.getInstance().getValueByKey("req_url");
			register.setAddress(ssoPath);
			if (ssoPath.startsWith("https:"))
				register.setCallType("https");
			else {
				register.setCallType("http");
			}
		}
		return register;
	}

}
